package cn.tedu.egnotice1.controller;

import cn.tedu.egnotice1.mapper.NoticeMapper;
import cn.tedu.egnotice1.pojo.dto.NoticeAddParam;
import cn.tedu.egnotice1.pojo.dto.NoticeAddQurey;
import cn.tedu.egnotice1.pojo.dto.NoticeUpdateParam;
import cn.tedu.egnotice1.pojo.entiy.Notice;
import cn.tedu.egnotice1.pojo.vo.NoticeDetailInfoVO;
import cn.tedu.egnotice1.pojo.vo.NoticeListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @PostMapping("v1/notice/add")
    @ResponseBody
    public String add(NoticeAddParam noticeAddParam){
        Notice notice=new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        //处理其他属性
        notice.setUserId(100L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        int i=noticeMapper.insertNotice(notice);
        if (i>0){
            return "新增资讯成功";
        }
        return "新增资讯失败";

    }
    /**
     * 资讯列表
     */
    @GetMapping("v1/notice/list")
    @ResponseBody
    public List<NoticeListVO> list(NoticeAddQurey noticeAddQurey){
        System.out.println("noticeAddQuery = "+noticeAddQurey);
        String title = noticeAddQurey.getTitle();
        Integer type = noticeAddQurey.getType();
        List<NoticeListVO> noticeListVOS=
                noticeMapper.selectNotice(title,type);
        return noticeListVOS;
    }

    /**
     * 资讯详情
     */
@GetMapping("v1/notice/detail")
    @ResponseBody
    public NoticeDetailInfoVO detail(Long id){
    System.out.println("id = "+id);
    NoticeDetailInfoVO noticeDetailInfoVO=
            noticeMapper.selectById(id);
    return noticeDetailInfoVO;
}


/**
 * 删除资讯
 */
@DeleteMapping("v1/notice/del")
    @ResponseBody
    public String del(Long id){
    int i=noticeMapper.deleteById(id);
    if (i>0) {
        return "删除公告成功";
    }
    return "该公告不存在";
}


/**
 * 更新资讯
 */
@PostMapping("v1/notice/update")
    @ResponseBody
    public String update(NoticeUpdateParam noticeUpdateParam){
    System.out.println("noyiceUpdateParam = "+noticeUpdateParam);
    Notice notice=new Notice();
    //复制属性
    BeanUtils.copyProperties(noticeUpdateParam,notice);
    //处理其他属性
    notice.setUpdateTime(new Date());
    int i=noticeMapper.UpdateById(notice);
    if (i>0){
        return "更新资讯成功";
    }
    return "该资讯不存在";
}














}
